<?php
class SalestransDetailsController extends GxController
{
    public function actionCreate()
    {
        $model = new SalestransDetails;
        if (!Yii::app()->request->isAjaxRequest)
            return;
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['SalestransDetails'][$k] = $v;
            }
            $model->attributes = $_POST['SalestransDetails'];
            $msg = "Data gagal disimpan.";
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->salestrans_details;
            } else {
                $msg .= " " . implode(", ", $model->getErrors());
                $status = false;
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate()
    {
        if (Yii::app()->request->isPostRequest) {
            $status = false;
            $msg = 'Failed update data.';
            if (isset($_POST) && !empty($_POST)) {
                $id = $_POST['salestrans_details'];
                $beauty_id = $_POST['beauty_id'];
                $jasa_dokter = get_number($_POST['jasa_dokter']);
                $model = $this->loadModel($id, 'SalestransDetails');
                $model->beauty_id = $beauty_id;
                $model->jasa_dokter = $jasa_dokter;
                app()->db->autoCommit = false;
                $transaction = Yii::app()->db->beginTransaction();
                try {
                    if (!$model->save())
                    {
                        throw new Exception(CHtml::errorSummary($model));
                    }else{
                        $model->save_beauty_tips();
                        $msg = 'Success update.';
                    }
                    $transaction->commit();
                    $status = true;
                } catch (Exception $ex) {
                    $transaction->rollback();
                    $status = false;
                    $msg = $ex->getMessage();
                }
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(403,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $msg = 'Data berhasil dihapus.';
            $status = true;
            try {
                $this->loadModel($id, 'SalestransDetails')->delete();
            } catch (Exception $ex) {
                $status = false;
                $msg = $ex;
            }
            echo CJSON::encode(array(
                'success' => $status,
                'msg' => $msg));
            Yii::app()->end();
        } else
            throw new CHttpException(400,
                Yii::t('app', 'Invalid request. Please do not repeat this request again.'));
    }
    public function actionIndex()
    {

        $criteria = new CDbCriteria();
        $criteria->addCondition('salestrans_id = :salestrans_id');
        $criteria->params = array(":salestrans_id"=>$_POST['salestrans_id']);
        $model = SalestransDetails::model()->findAll($criteria);
        $total = SalestransDetails::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}